//
//  LDWorthCollectionFlowLayout.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDWorthCollectionFlowLayout: UICollectionViewFlowLayout {

    override init() {
        super.init()
        scrollDirection = .Vertical
//        headerReferenceSize = CGSizeMake(LDFrame.ScreenWidth, 240)
    }
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }
    
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
    
            let offset = collectionView!.contentOffset
            let attributes = super.layoutAttributesForElementsInRect(rect)
        
            if (offset.y<0) {
               let deltaY:CGFloat = fabs(offset.y)
                
                for attrs in attributes! {
                    
                    let kind = attrs.representedElementKind
                    if (kind == UICollectionElementKindSectionHeader) {
                        
                        let headerSize = self.headerReferenceSize
                        var headRect = attrs.frame
                        headRect.size.height = headerSize.height + deltaY;
                        headRect.size.width = headerSize.width + deltaY;
                        headRect.origin.y = headRect.origin.y - deltaY;
                        headRect.origin.x = headRect.origin.x - deltaY/2;
                        attrs.frame = headRect
                    }
                }
            }
        
            return attributes;
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
